s#include "sys_config.h"

SysConfig* SysConfig::instance_ = NULL;

SysConfig* SysConfig::instance()
{
    if (instance_ == NULL)
        instance_ = new SysConfig();

    return instance_;
}

int SysConfig::LoadConfigFile(const string& filename)
{
	config_file_ = filename;

    config_heap_ = new ACE_Configuration_Heap();
    config_heap_->open();
    ACE_Ini_ImpExp conf_imp(*config_heap_);
    if (conf_imp.import_config(filename.c_str()) != 0) {
        ACE_DEBUG((LM_ERROR, "[%D] open config file: %s failed\n", filename.c_str()));
        return -1;
    }

	ACE_Configuration_Section_Key section;
	ACE_TString section_name;
	
	for (int j = 0; ; j++) {
		if (config_heap_->enumerate_sections(config_heap_->root_section(), j, section_name) != 0)
            break;

		if (config_heap_->open_section(config_heap_->root_section(),
            							   section_name.c_str(), 0, section) != 0) {
            ACE_DEBUG((LM_ERROR, "[%D] open %s section failed\n",
                       section_name.c_str()));
            return -1;
        }

		ACE_DEBUG((LM_TRACE, "[%D] reading section: %s\n", section_name.c_str()));

		SECTION_CONF_MAP section_conf;
		ACE_TString config_name;
		ACE_TString config_value;
		ACE_Configuration::VALUETYPE value_type;
		
		for (int i = 0; ; i++) {
            if (config_heap_->enumerate_values(section, i, config_name, value_type) != 0)
                break;

            if (config_heap_->get_string_value(section, config_name.c_str(), config_value) != 0) {
                ACE_DEBUG((LM_ERROR, "[%D] read config: %d failed\n",
                           config_name.c_str()));
                return -1;
            }

            section_conf[config_name.c_str()] = config_value.c_str();

            ACE_DEBUG((LM_TRACE, "[%D] config: %s = %s\n",
                       config_name.c_str(), config_value.c_str()));
        }

        config_items_[section_name.c_str()] = section_conf;
	}
	

    ACE_DEBUG((LM_INFO, "[%D] system config load succ\n"));
    return 0;
}

int SysConfig::get_conf_int(const char* section, const char* name, int* value)
{
    CONFIG_MAP::const_iterator iter = config_items_.find(section);
    if (iter != config_items_.end()) {
        SECTION_CONF_MAP::const_iterator it = iter->second.find(name);
        if (it != iter->second.end()) {
            *value = atoi(it->second.c_str());
            return 0;
        }
    }

    return -1;
}

int SysConfig::get_conf_str(const char* section, const char* name,
                          char* value, int buf_len)
{
    CONFIG_MAP::const_iterator iter = config_items_.find(section);
    if (iter != config_items_.end())
    {
        SECTION_CONF_MAP::const_iterator it = iter->second.find(name);
        if (it != iter->second.end()) {
            strncpy(value, it->second.c_str(), buf_len);
            value[buf_len - 1] = '\0';
            return 0;
        }
    }

    return -1;
}

int SysConfig::write_conf_int(const char* section, const char* name, int value)
{
    char value_str[11];
    sprintf(value_str, "%d", value);

    return write_conf_str(section, name, value_str);
}

int SysConfig::write_conf_str(const char* section, const char* name, char* value)
{
    config_items_[section][name] = value;

    ACE_Configuration_Section_Key section_key;
    config_heap_->open_section(config_heap_->root_section(),
                               section, 1, section_key);
    config_heap_->set_string_value(section_key, name, value);

    ACE_Ini_ImpExp conf_imp(*config_heap_);
    if (conf_imp.export_config(config_file_.c_str()) != 0) {
        ACE_DEBUG((LM_ERROR, "[%D] write config failed: [%s] %s = %s\n",
                   section, name, value));
        return -1;
    }

    ACE_DEBUG((LM_TRACE, "[%D] write config succ: [%s] %s = %s\n",
               section, name, value));

    return 0;
}

